package com.hawthorn.admin.repository.provider;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hawthorn.component.constant.DicConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.builder.annotation.ProviderMethodResolver;

import java.util.Map;

/**
 * <p>todo 字典管理SQL拼接</p>
 * @author: guohao | 2607728024@qq.com
–
 * @created: 2022/7/1 11:40
 * @version v1.0.1
 * @copyright: Copyright (c) 2022 guohao
 */
@Slf4j
public class SysDictSqlProvider implements ProviderMethodResolver
{

  /**
   * <p>todo 字典管理SQL</p>
   * @param conditions
   * @param page
   * @return: java.lang.String
   * @author: guohao | 2607728024@qq.com
   * @date: 2022/7/1 11:48
   * @version: 1.0.1
   * Modification History:
   * Date Author Version Description
   * -----------------------------------------------------------
   * 2022/7/1 guohao v1.0.1 init
   */
  public String selectDictList(String conditions, Page<Map<String, Object>> page)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select\n ");
    sql.append(" 	parent,\n ");
    sql.append(" 	type_name 	typeName,\n ");
    sql.append(" 	type \n ");
    sql.append(" from\n ");
    sql.append(" 	sys_dict \n ");
    sql.append(" where \n ");
    sql.append(conditions);
    sql.append("and status = " + DicConstant.YXBS_01 + "  \n");
    sql.append(" group by\n ");
    sql.append(" 	parent,\n ");
    sql.append(" 	type_name,\n ");
    sql.append(" 	type\n ");
    return sql.toString();
  }

  /**
   * <p>todo 根据父级ID获取子级列表</p>
   * @param conditions
   * @param page
   * @return: java.lang.String
   * @author: guohao | 2607728024@qq.com
   * @date: 2022/7/1 14:15
   * @version: 1.0.1
   * Modification History:
   * Date Author Version Description
   * -----------------------------------------------------------
   * 2022/7/1 guohao v1.0.1 init
   */
  public String selectChildListByParent(String conditions, Page<Map<String, Object>> page)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select\n ");
    sql.append(" 	id,\n ");
    sql.append(" 	dic_code 	dicCode,\n ");
    sql.append(" 	parent,\n ");
    sql.append(" 	type_name typeName,\n ");
    sql.append(" 	type ,\n ");
    sql.append(" 	label,\n ");
    sql.append(" 	spell,\n ");
    sql.append(" 	aspell,\n ");
    sql.append(" 	label_en 	labelEn,\n ");
    sql.append(" 	status, \n ");
    sql.append(" 	sort \n ");
    sql.append(" from\n ");
    sql.append(" 	sys_dict\n ");
    sql.append(" where \n ");
    sql.append(conditions);
    sql.append("and dic_code is not null \n");
    sql.append("and (status != 100202 or status is null) \n");
    sql.append(" order by sort \n");
    return sql.toString();
  }

  /**
   * <p>todo 根据父ID查找字典项</p>
   * @param parent
   * @return: java.lang.String
   * @author: guohao | 2607728024@qq.com
   * @date: 2022/7/1 17:41
   * @version: 1.0.1
   * Modification History:
   * Date Author Version Description
   * -----------------------------------------------------------
   * 2022/7/1 guohao v1.0.1 init
   */
  public String selectDictByParent(String parent)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select\n ");
    sql.append(" 	id,\n ");
    sql.append(" 	dic_code 	dicCode,\n ");
    sql.append(" 	type ,\n ");
    sql.append(" 	type_name typeName,\n ");
    sql.append(" 	parent, \n ");
    sql.append(" 	`status` \n ");
    sql.append(" from\n ");
    sql.append(" 	sys_dict\n ");
    sql.append(" where  ");
    sql.append("parent = ");
    sql.append(parent);
    sql.append("and status != 100202 \n");
    return sql.toString();
  }

  /**
   * <p>todo 拼接Where条件查询字典</p>
   * @param conditions
   * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
   * @author: guohao | 2607728024@qq.com
   * @date: 2022/7/4 14:36
   * @version: 1.0.1
   * Modification History:
   * Date Author Version Description
   * -----------------------------------------------------------
   * 2022/7/4 guohao v1.0.1 init
   */
  public String selectDictByWhere(String conditions)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select\n ");
    sql.append(" 	id,\n ");
    sql.append(" 	dic_code 	dicCode,\n ");
    sql.append(" 	parent,\n ");
    sql.append(" 	label,\n ");
    sql.append(" 	spell,\n ");
    sql.append(" 	aspell,\n ");
    sql.append(" 	is_leaf 	isLeaf,\n ");
    sql.append(" 	label_en 	labelEn,\n ");
    sql.append(" 	sort \n ");
    sql.append(" from\n ");
    sql.append(" 	sys_dict\n ");
    sql.append(" where \n ");
    sql.append(conditions);
    sql.append("and status != 100202 \n");
    return sql.toString();
  }

  // 根据字典代码查询字典项
  public String selectDictByCode(String dictCode)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select\n ");
    sql.append(" 	id,\n ");
    sql.append(" 	dic_code 	dicCode,\n ");
    sql.append(" 	parent\n ");
    sql.append(" from\n ");
    sql.append(" 	sys_dict\n ");
    sql.append(" where  ");
    sql.append("dic_code = ");
    sql.append(dictCode);
    sql.append(" and status = " + DicConstant.YXBS_01);
    return sql.toString();
  }

  /**
   * <p>todo 获取最大父级代码</p>
   * @param
   * @return: java.lang.String
   * @author: guohao | 2607728024@qq.com
   * @date: 2022/7/5 17:17
   * @version: 1.0.1
   * Modification History:
   * Date Author Version Description
   * -----------------------------------------------------------
   * 2022/7/5 guohao v1.0.1 init
   */
  public String selectMaxParentCode()
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select\n ");
    sql.append("   max( parent )\n ");
    sql.append("   from\n ");
    sql.append("       sys_dict\n ");
    return sql.toString();
  }

  /**
   * <p>todo 获取子级最大代码</p>
   * @param
   * @return: java.lang.String
   * @author: guohao | 2607728024@qq.com
   * @date: 2022/7/5 17:22
   * @version: 1.0.1
   * Modification History:
   * Date Author Version Description
   * -----------------------------------------------------------
   * 2022/7/5 guohao v1.0.1 init
   */
  public String selectMaxChildCode(Integer parent)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" select\n ");
    sql.append("   max( dic_code )\n ");
    sql.append("   from\n ");
    sql.append("       sys_dict\n ");
    sql.append(" where parent =  " + parent);
    return sql.toString();
  }
}
